#include "FileUtils.h"

#include "AssertUtils.h"
#include "LogUtils.h"

#include <fstream>

namespace FileUtils
{

const Json::Value ParseJsonFromFile(const std::string& filepath)
{
	std::ifstream file(filepath);
	Json::Value root;

	if (!file.is_open())
	{
		Log::Error("Couldn't open json file!");
		assert(0);
		return root;
	}

	Json::Reader reader;
	bool parsed = reader.parse(file, root, false);
	file.close();

	if (!parsed)
	{
		Log::Error("Parse failed for json file %s!", filepath.c_str());
		assert(0);
	}

	return root;
}

bool WriteJsonToFile(const Json::Value& json, const std::string& filepath)
{
	std::ofstream file(filepath);

	if (!file.is_open())
	{
		Log::Error("Couldn't open file!");
		assert(0);
		return false;
	}

	Json::StyledStreamWriter writer;
	writer.write(file, json);
	return true;
}

}